#-*- coding: UTF-8 -*- 
from selenium import webdriver
import unittest,time,random
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import time, logging,os
import sys
reload(sys)
sys.setdefaultencoding('utf8')



driver = webdriver.Firefox()
driver.implicitly_wait(6)

driver.get("http://atwork.workplus.io/workplus/login;JSESSIONID=ad4e819c-0668-4089-bb4c-b01384c64642#statistics")

#https://dashboard3.workplus.io/index.jsp#orgs/list   product
#http://atwork.workplus.io/workplus/login;JSESSIONID=ad4e819c-0668-4089-bb4c-b01384c64642#statistics 
#test

username_loc = (By.NAME,  "username")
password_loc = (By.NAME,  "password")
submit_loc = (By.ID, "accountLoginBtn")

modify_banks = (By.XPATH, "//*[@data-rel='tooltip']/i")

modify_and_sendkys = (By.XPATH, "//*[@id='addOrgModal']/div[2]/div/form/div[1]/div[1]/div[1]/input")

modify_message = (By.XPATH, "//p[@class='ng-binding']")

upload_photo = (By.ID, "orgLogoInput")

dismiss_org = (By.XPATH, ".//*[@id='addOrgModal']/div[2]/div/form/div[2]/button[1]")
dismiss_org_sure = (By.XPATH, "/html/body/div[4]/div[2]/div/div[2]/button[2]")

address_manage = (By.LINK_TEXT, u"通讯录管理")
click_message = (By.CLASS_NAME, "custom-plus")
add_employee = (By.ID, "add-employee-btn")
click_accountName = (By.NAME, "accountName")
employee_append = (By.ID, "employee-append-name")
employee_positions = (By.ID, "employee-positions-0")
jstree_wholerow = (By.XPATH, "//*[@id='select-tree']/ul/li/a/i[1]")
#.//*[@id='append-selection-set']/span
employee_append_confirm = (By.ID, "employee-append-confirm")
email_name = (By.NAME, "email")
employee_jobTitle = (By.NAME, "employee-jobTitle-0")

random_pw = random.randint(100000,900000)
random_str = random.choice("1234567890")
del_account = (By.CSS_SELECTOR, ".pic-icon.delete-icon")
#html/body/div[54]/div[2]/div/div[2]/button[2]
#.//*[@id='employees-table']/tr[1]/td[5]/a[3]/i
#
logging.basicConfig(filename = os.path.join(os.getcwd(), 'log.txt'),
                        level=logging.INFO, filemode='a', format='%(asctime)s - %(levelname)s: %(message)s')



employees_table = (By.ID, "employees-table")
employees_table_row = (By.XPATH, "row")
get_message_name = (By.CLASS_NAME, "odd")


sure_del = "body > div.bootbox.modal.fade.bootbox-confirm.in > div.modal-dialog > div > div.modal-footer > button.btn.btn-success.btn-sm.btn-atwork"
make_sure_del = (By.CSS_SELECTOR, sure_del)

test_tel = "18888%d" % (random_pw)

def find_element(loc, wait=15):
    try:
        WebDriverWait(
                driver, wait, 0.5).until(
                lambda driver: driver.find_element(
						*loc).is_displayed())
        return driver.find_element(*loc)
    except:
        print "not such element"

def find_elements(loc):
    try:
        if len(driver.find_elements(*loc)):
        	return driver.find_elements(*loc)
    except:
        print "not such element"


#/html/body/div[4]/div[2]/div/div[2]/button[2]
driver.find_element(*username_loc).send_keys("15986301302")
driver.find_element(*password_loc).send_keys("123456")
driver.find_element(*submit_loc).click()
time.sleep(1)

logging.warning('login in success')

try:
	t = driver.find_elements(*modify_message)
	logging.info('find_element:' + "modify_message:" +str(modify_message))
	#print t
	if t is not None:
		for i in t:
			#time.sleep(1)
			i.click()


except NoSuchElementException as e :
	logging.error('not sunch element' + str(modify_message))
	pass




time.sleep(1)
driver.find_element(*address_manage).click()
driver.find_element(*click_message).click()
time.sleep(1)
driver.find_element(*add_employee).click()
driver.find_element(*click_accountName).send_keys(test_tel)
time.sleep(1)
driver.find_element(*employee_append).send_keys("test")
time.sleep(1)
driver.find_element(*employee_positions).click()
time.sleep(1)
driver.find_element(*jstree_wholerow).click()
time.sleep(1)
driver.find_element(*employee_append_confirm).click()
driver.find_element(*employee_jobTitle).send_keys("test")

time.sleep(1)
driver.find_element(*email_name).send_keys("test@test.com")
time.sleep(1)
driver.find_element(*employee_append_confirm).click()
time.sleep(1)



wrap_div = driver.find_element(*employees_table)
print "wrap_div:", wrap_div

time.sleep(1)
card = wrap_div.find_element(*get_message_name)
time.sleep(1)
card.find_element(*del_account).click()
time.sleep(3)
driver.find_element(*make_sure_del).click()
time.sleep(30)

print "end"
driver.quit()
